﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Nvigorate.Data;

namespace Nvigorate.Test.Data
{
    /// <summary>
    /// Summary description for DataInterfaceTests
    /// </summary>
    [TestClass]
    public class DataInterfaceTests
    {
        [TestMethod]
        public void TestExecuteCommandWithNoParametersAndNoTransaction()
        {
            var mockFactory = new MockFactory(MockBehavior.Default);
            var commandText = "SELECT * FROM Employee";

            var connection = mockFactory.Create<IDbConnection>();
            var providerConnection = mockFactory.Create<IDbConnection>();
            var provider = mockFactory.Create<IDatabaseSpecificProvider>();
            var command = mockFactory.Create<IDatabaseCommand>();
            var commandFactory = mockFactory.Create<IDatabaseCommandFactory>();

            commandFactory.Expect(factory => factory.GetCommand(commandText, provider.Object)).Returns(command.Object);
            command.Expect(
                cmd =>
                cmd.SetParameters(It.Is<IEnumerable<IDbDataParameter>>(p => p.Count() == 0)));

            provider.Expect(prv => prv.GetConnection(connection.Object)).Returns(providerConnection.Object);
            command.Expect(cmd => cmd.Execute(providerConnection.Object, false));

            var dataInterface = new DataInterface(connection.Object, provider.Object, commandFactory.Object);
            dataInterface.ExecuteCommand(commandText, false);

            mockFactory.VerifyAll();
        }
    }
}